package p2p.filesharing
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.StatusEvent;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import p2p.model.vo.DescriptorVO;
	import p2p.model.vo.P2PSharedVO;
	import p2p.model.vo.SharingInfo;
	
	[Event(name="complete",type="flash.events.Event")]
	[Event(name="status",type="flash.events.StatusEvent")]
	public class LocalFileLoader extends EventDispatcher
	{
		private var file:FileReference;
		
		public var p2pSharedObject:P2PSharedVO;
		
		public function LocalFileLoader() 
		{
			file = new FileReference();
			file.addEventListener(Event.SELECT, selectHandler);
			file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler)
			file.addEventListener(Event.COMPLETE, completeHandler);
		}
		
		public function browseFileSystem() : void 
		{
			file.browse();
		}
		
		protected function selectHandler(event:Event) : void 
		{
			sendStatusEvent("fileChosen " + file.name + " | " + file.size);
			
			trace("vitalik:","fileChosen " + file.name + " | " + file.size)
			
			file.load();
		}
		
		protected function ioErrorHandler(event:IOErrorEvent) : void 
		{
			sendStatusEvent("ioErrorHandler: " + event);
		}
		
		protected function securityErrorHandler(event:SecurityErrorEvent) : void 
		{
			sendStatusEvent("securityError: " + event);
		}
		
		protected function progressHandler(event:ProgressEvent) : void 
		{
			var file:FileReference = FileReference(event.target);
			sendStatusEvent("progressHandler: bytesLoaded=" + event.bytesLoaded + "/" +event.bytesTotal);
		}
		
		protected function completeHandler(event:Event) : void 
		{
			sendStatusEvent("completeHandler");
			
			p2pSharedObject = new P2PSharedVO();
			p2pSharedObject.data = file.data;
			
			var descriptor:DescriptorVO = new DescriptorVO();
			descriptor.filename = file.name;
			descriptor.size = file.size;
			
			//start and end indexes setting up
			var startIdx:int = SharingInfo.getInstance().endBusyChunkIndex + 1;
			var endIdx:int = startIdx + descriptor.packetLength - 1;
			descriptor.startChunkIndex = startIdx;
			descriptor.endChunkIndex = endIdx;
			SharingInfo.getInstance().endBusyChunkIndex = endIdx;
			
			p2pSharedObject.descriptor = descriptor;			
			
			//write full chunks
			for(var i:int = startIdx; i < endIdx; i++)
			{
				p2pSharedObject.chunks[i] = new ByteArray();
				p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i], 0, DescriptorVO.DEFAULT_PACKET_SIZE_BYTES);
				
			}
			// +1 last not full chunk
			p2pSharedObject.chunks[i] = new ByteArray();
			p2pSharedObject.data.readBytes(p2pSharedObject.chunks[i], 0, p2pSharedObject.data.bytesAvailable);
			
			sendStatusEvent("----- p2pSharedObject -----");
			sendStatusEvent("packetLenght: "+(p2pSharedObject.descriptor.packetLength));
			
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		protected function sendStatusEvent(str:String) : void
		{
			var e:StatusEvent = new StatusEvent(StatusEvent.STATUS, false, false, "status", str);
			
			dispatchEvent(e);
		}
	}
}